package i_iterator.a_design_pattern;

/**
 * Created by wug on 2016/7/13 0013.
 */
public class MyArrayList<T> implements MyCollection {

    private Object[] arr = new Object[10];
    private int index = 0;

    public void add(T t) {
        if (index >= arr.length) {
            Object[] temp = arr;
            arr = new Object[arr.length + 10];
            for (int i = 0; i < temp.length; i++) {
                arr[i] = temp[i];
            }
        }
        arr[index] = t;
        index++;
    }

    public T get(int i) {
        return (T)arr[i];
    }

    public int size() {
        return index;
    }

    public MyIterator<T> iterator() {
        return new MyArrListIterator<>(arr);
    }

    public MyArrayListFilterIterator<T> filterIterator() {
        return new MyArrayListFilterIterator(arr);
    }
}
